// pages/operation/points/pointsProvide/index.js
import formRules from "../../../../utils/rules-form.js"
import {
  Points
} from "../../../../api/api-work/points.js"
const points = new Points()
import Toast from '@vant/weapp/toast/toast';
const rules = {
  remark: [{
    required: true,
    message: '请输入记录内容'
  }],
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    delta: 2,
    studentId: "",
    remark: "",
    type: "1",
    score: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("sssss: ", options)
    // const params = JSON.parse(options.param);
    this.setData({
      studentId: options.param,
      delta: options.delta
    })
    // console.log('params', params);
  },
  onChange1(e) {
    this.setData({
      remark: e.detail
    })
  },
  changeType(e) {
    this.setData({
      type: e.target.dataset.query
    })
  },
  onChange2(e) {
    this.setData({
      score: e.detail
    })
  },

  async savePoints() {
    const params = {
      studentId: this.data.studentId,
      score: this.data.type === "1" ? this.data.score : this.data.score * -1,
      remark: this.data.remark,
    }
    formRules(rules, this.data, (status) => {
      if (status) {
        points.savePointsAPI(params).then(res => {
          Toast('积分发放成功');
          setTimeout(() => {
            if (this.data.delta == 1) {
              wx.navigateBack()
            } else {
              wx.navigateBack({
                delta: 2,
              });
            }
          }, 1000);
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})